import api from './auth'

export const translateToSql = async (text) => {
  try {
    const response = await api.post('/api/translate', { text })
    return response.data
  } catch (error) {
    throw new Error(error.response?.data?.message || 'SQL转换失败')
  }
}

export const translateToSqlWithConnection = async (text, connectionId) => {
  try {
    const response = await api.post('/api/translate/with-connection', { text, connectionId })
    return response.data
  } catch (error) {
    throw new Error(error.response?.data?.message || 'SQL转换失败')
  }
}

export const getHistory = async (page = 1, pageSize = 10) => {
  try {
    const response = await api.get(`/api/translate/history?page=${page}&pageSize=${pageSize}`)
    return response.data
  } catch (error) {
    throw new Error(error.response?.data?.message || '获取历史记录失败')
  }
}

export const executeSql = async (sql) => {
  try {
    const cleanSql = sql.trim().replace(/;+$/, '')
    const response = await api.post('/api/translate/execute', { sql: cleanSql })
    return response.data
  } catch (error) {
    throw new Error(error.response?.data?.message || 'SQL执行失败')
  }
}

export const executeSqlWithConnection = async (sql, connectionId) => {
  try {
    const cleanSql = sql.trim().replace(/;+$/, '')
    const response = await api.post('/api/translate/execute-with-connection', { 
      sql: cleanSql,
      connectionId
    })
    return response.data
  } catch (error) {
    throw new Error(error.response?.data?.message || 'SQL执行失败')
  }
}

export const getExecutions = async (limit = 50) => {
  try {
    const response = await api.get(`/api/translate/executions?limit=${limit}`)
    return response.data
  } catch (error) {
    throw new Error(error.response?.data?.message || '获取SQL执行记录失败')
  }
} 